package com.brainhealth.customer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.brainhealth.common.core.domain.TreeSelect;
import com.brainhealth.common.enums.LearnCatetoryType;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Learn;
import com.brainhealth.customer.mapper.LearnMapper;
import com.brainhealth.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.LearnCategoryMapper;
import com.brainhealth.customer.domain.LearnCategory;
import com.brainhealth.customer.service.ILearnCategoryService;

/**
 * 学习库分类树Service业务层处理
 * 
 * @author wolong
 * @date 2023-04-23
 */
@Service
public class LearnCategoryServiceImpl implements ILearnCategoryService 
{
    @Autowired
    private LearnCategoryMapper learnCategoryMapper;
    @Autowired
    private LearnMapper learnMapper;

    /**
     * 查询学习库分类树
     * 
     * @param learnCategoryId 学习库分类树主键
     * @return 学习库分类树
     */
    @Override
    public LearnCategory selectLearnCategoryByLearnCategoryId(Long learnCategoryId)
    {
        return learnCategoryMapper.selectLearnCategoryByLearnCategoryId(learnCategoryId);
    }

    /**
     * 查询学习库分类树列表
     * 
     * @param learnCategory 学习库分类树
     * @return 学习库分类树
     */
    @Override
    public List<LearnCategory> selectLearnCategoryList(LearnCategory learnCategory)
    {
        List<LearnCategory> lstData = learnCategoryMapper.selectLearnCategoryList(learnCategory);
        lstData.forEach(item->item.setCategoryName(item.getName()));
        return lstData;
    }

    @Override
    public List<LearnCategory> getLearnCategoryLeaf()
    {
        LearnCategory learnCategory = new LearnCategory();
        List<LearnCategory> lstData = learnCategoryMapper.selectLearnCategoryList(learnCategory);
        List<LearnCategory> lstLeaf = new ArrayList<>();
        for (LearnCategory node : lstData)
        {
            List<LearnCategory> lstChild = lstData.stream().filter(item -> item.getParentId() == node.getLearnCategoryId()).collect(Collectors.toList());
            if (lstChild.size() == 0)
            {
                lstLeaf.add(node);
            }
        }
        return lstLeaf;
    }

    @Override
    public List<LearnCategory> getLearnCategoryList(LearnCatetoryType categoryType)
    {
        Learn queryLearn = new Learn();
        List<Learn> lstLearn = learnMapper.selectLearnList(queryLearn);
        LearnCategory learnCategory = new LearnCategory();
        learnCategory.setCategoryType(categoryType);
        List<LearnCategory> lstLearnCategory = learnCategoryMapper.selectLearnCategoryList(learnCategory);
        List<LearnCategory> lstData = new ArrayList<>();
        for (LearnCategory category : lstLearnCategory)
        {
            Learn tmpData = lstLearn.stream().filter(item -> item.getLearnCategoryId() == category.getLearnCategoryId()).findFirst().orElse(null);
            if (tmpData != null)
            {
                lstData.add(category);
            }
        }
        return lstData;
    }

    /**
     * 新增学习库分类树
     * 
     * @param learnCategory 学习库分类树
     * @return 结果
     */
    @Override
    public int insertLearnCategory(LearnCategory learnCategory)
    {
        learnCategory.setCreateTime(DateUtils.getNowDate());
        return learnCategoryMapper.insertLearnCategory(learnCategory);
    }

    /**
     * 修改学习库分类树
     * 
     * @param learnCategory 学习库分类树
     * @return 结果
     */
    @Override
    public int updateLearnCategory(LearnCategory learnCategory)
    {
        learnCategory.setUpdateTime(DateUtils.getNowDate());
        return learnCategoryMapper.updateLearnCategory(learnCategory);
    }

    /**
     * 批量删除学习库分类树
     * 
     * @param learnCategoryIds 需要删除的学习库分类树主键
     * @return 结果
     */
    @Override
    public int deleteLearnCategoryByLearnCategoryIds(Long[] learnCategoryIds)
    {
        return learnCategoryMapper.deleteLearnCategoryByLearnCategoryIds(learnCategoryIds);
    }

    /**
     * 删除学习库分类树信息
     * 
     * @param learnCategoryId 学习库分类树主键
     * @return 结果
     */
    @Override
    public int deleteLearnCategoryByLearnCategoryId(Long learnCategoryId)
    {
        return learnCategoryMapper.deleteLearnCategoryByLearnCategoryId(learnCategoryId);
    }

    @Override
    public List<TreeSelect> getLearnCategoryTree()
    {
        LearnCategory queryLearnCategory = new LearnCategory();
        List<LearnCategory> lstData = learnCategoryMapper.selectLearnCategoryList(queryLearnCategory);
        List<TreeSelect> lstCategoryTree = new ArrayList<>();
        for (LearnCategory category : lstData)
        {
            if (category.getParentId() == 0)
            {
                TreeSelect root = new TreeSelect();
                root.setId(category.getLearnCategoryId());
                root.setLabel(category.getName());
                lstCategoryTree.add(root);
                getCategoryChildren(root, lstData);
            }
        }
        return lstCategoryTree;
    }

    public void getCategoryChildren(TreeSelect node, List<LearnCategory> lstAllData)
    {
        List<LearnCategory> lstChildren = lstAllData.stream().filter(item -> item.getParentId() == node.getId()).collect(Collectors.toList());
        if (lstChildren.size() > 0)
        {
            for (LearnCategory childData: lstChildren)
            {
                TreeSelect childNode = new TreeSelect();
                childNode.setId(childData.getLearnCategoryId());
                childNode.setLabel(childData.getName());
                node.getChildren().add(childNode);
            }
        }
        for (TreeSelect child : node.getChildren())
        {
            getCategoryChildren(child, lstAllData);
        }
    }

}
